<?php
function _cutstr2($string, $length, $dot = ' ...', $htmlencode = true, $charset = 'utf-8') {
	if (strlen ( $string ) <= $length) {
		return $string;
	}
	if ($htmlencode)
		$string = str_replace ( array ('&amp;', '&quot;', '&lt;', '&gt;' ), array ('&', '"', '<', '>' ), $string );
	$strcut = '';
	if (strtolower ( $charset ) == 'utf-8') {
		$n = $tn = $noc = 0;
		while ( $n < strlen ( $string ) ) 
		{
			$t = ord ( $string [$n] );
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1;
				$n ++;
				$noc ++;
			} elseif (194 <= $t && $t <= 223) {
				$tn = 2;
				$n += 2;
				$noc += 2;
			} elseif (224 <= $t && $t < 239) {
				$tn = 3;
				$n += 3;
				$noc += 2;
			} elseif (240 <= $t && $t <= 247) {
				$tn = 4;
				$n += 4;
				$noc += 2;
			} elseif (248 <= $t && $t <= 251) {
				$tn = 5;
				
				$n += 5;
				$noc += 2;
			} elseif ($t == 252 || $t == 253) {
				$tn = 6;
				$n += 6;
				$noc += 2;
			} else {
				$n ++;
			}
			if ($noc >= $length) {
				break;
			}
		}
		if ($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr ( $string, 0, $n );
	} else {
		for($i = 0; $i < $length; $i ++) {
			$strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
		}
	}
	if ($htmlencode)
		$strcut = str_replace ( array ('&', '"', 

		'<', '>' ), array ('&amp;', '&quot;', '&lt;', '&gt;' ), $strcut );
	return $strcut . (strlen ( $string ) > strlen ( $strcut ) ? $dot : '');
}
//截取字符串个数
function _cutstr($string, $length, $dot = '...') {
	global $charset;
	if (strlen ( $string ) <= $length) {
		return $string;
	}
	
	$strcut = '';
	if (strtolower ( $charset ) == 'utf-8') {
		
		$n = $tn = $noc = 0;
		while ( $n < strlen ( $string ) ) {
			
			$t = ord ( $string [$n] );
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1;
				$n ++;
				$noc ++;
			} elseif (194 <= $t && $t <= 223) {
				$tn = 2;
				$n += 2;
				$noc += 2;
			} elseif (224 <= $t && $t < 239) {
				$tn = 3;
				$n += 3;
				$noc += 2;
			} elseif (240 <= $t && $t <= 247) {
				$tn = 4;
				$n += 4;
				$noc += 2;
			} elseif (248 <= $t && $t <= 251) {
				$tn = 5;
				$n += 5;
				$noc += 2;
			} elseif ($t == 252 || $t == 253) {
				$tn = 6;
				$n += 6;
				$noc += 2;
			} else {
				$n ++;
			}
			
			if ($noc >= $length) {
				break;
			}
		
		}
		if ($noc > $length) {
			$n -= $tn;
		}
		
		$strcut = substr ( $string, 0, $n );
	
	} else {
		for($i = 0; $i < $length - 3; $i ++) {
			$strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
		}
	}
	
	return $strcut . $dot;
}
//获取IP地址
function GetIP() {
	if (getenv ( 'HTTP_CLIENT_IP' )) {
		$ip = getenv ( 'HTTP_CLIENT_IP' );
	} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' )) { //获取客户端用代理服务器访问时的真实ip 地址
		$ip = getenv ( 'HTTP_X_FORWARDED_FOR' );
	} elseif (getenv ( 'HTTP_X_FORWARDED' )) {
		$ip = getenv ( 'HTTP_X_FORWARDED' );
	} elseif (getenv ( 'HTTP_FORWARDED_FOR' )) {
		$ip = getenv ( 'HTTP_FORWARDED_FOR' );
	} elseif (getenv ( 'HTTP_FORWARDED' )) {
		$ip = getenv ( 'HTTP_FORWARDED' );
	} else {
		$ip = $_SERVER ['REMOTE_ADDR'];
	}
	return $ip;
}
//建立新目录夹，已存在目录返回true
function n_mkdir($dir, $mode = 0777) {
	$u = umask ( 0 );
	if (n_file_exists ( $dir ))
		return true;
	$dir = n_path ( ROOTDIR . (substr ( $dir, 0, 1 ) == "/" ? "" : "/") . $dir );
	$r = @mkdir ( $dir, $mode );
	umask ( $u );
	return $r;
}
//创建新目录，允许多重目录. 例/abc/abc/abc.
function n_any_mkdir($dirs, $mode = 0777) {
	if (! strrpos ( $dirs, '/' )) {
		return (n_mkdir ( $dirs, $mode ));
	} else {
		$forder = explode ( '/', $dirs );
		$f = '';
		for($n = 0; $n < count ( $forder ); $n ++) {
			if ($forder [$n] == '')
				continue;
			$f .= ((($n == 0) && ($forder [$n] != '')) ? ('') : ('/')) . $forder [$n];
			if (n_file_exists ( $f )) {
				@chmod ( $f, 0777 );
				continue;
			} else {
				if (n_mkdir ( $f, $mode ))
					continue;
				else
					return false;
			}
		}
		return true;
	}
}
//删除文件
function n_file_unlink($resources) {
	if (! $resources)
		return false;
	$resources = n_path ( ROOTDIR . (substr ( $resources, 0, 1 ) == "/" ? "" : "/") . $resources );
	$ret = @unlink ( $resources );
	return $ret;
}
function _unlink($resources) {
	$resources = trim ( $resources );
	if (! $resources)
		return false;
	if (! is_file ( $resources ))
		return false;
	if (file_exists ( $resources )) {
		return @unlink ( $resources );
	} else
		return false;
}
// 上传文件函数 
// 说明 成功返回空值, 并取得返回值, 失败返回错误代码. 
function n_fileupload(&$data, $fieldname, $savepath = "/attachments/", $file_exps = array("gif","jpg","png")) {
	
	if ((! $_FILES [$fieldname] ["error"]) && ($_FILES [$fieldname] ["size"])) {
		if (is_chinese_filename ( $_FILES [$fieldname] ["name"] )) {
			@unlink ( $_FILES [$fieldname] ['tmp_name'] );
			return "error_file_name";
		}
		
		if (! file_exp ( $_FILES [$fieldname] ["name"], $file_exps )) {
			@unlink ( $_FILES [$fieldname] ['tmp_name'] );
			return "error_file_exp";
		}
		$newfilename = strtolower ( substr ( str_replace ( " ", "", microtime () ), 2 ) . "-" . $_FILES [$fieldname] ["name"] );
		if (! n_file_exists ( $savepath )) {
			if (! n_any_mkdir ( $savepath ))
				return "error_forlder_create";
		}
		$savepath = strtolower ( $savepath . $newfilename );
		if (! n_move_uploaded_file ( $_FILES [$fieldname] ['tmp_name'], $savepath )) {
			@unlink ( $_FILES [$fieldname] ['tmp_name'] );
			return "error_save";
		}
		$data = array ("filename" => $_FILES [$fieldname] ["name"], "savepath" => $savepath );
		return "";
	} else
		return "notfileupload";
}
//判断文件名中是否带有中文. 有返回true
function is_chinese_filename($str) {
	$ischinese = false;
	for($i = 0; $i < strlen ( $str ); $i ++) {
		if (ord ( $str [$i] ) > 0xa1) {
			$ischinese = true;
			break;
		}
	}
	return $ischinese;
}

function IsRobot() {
	if (! defined ( 'IS_ROBOT' )) {
		$kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
		$kw_browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
		if (preg_match ( "/($kw_browsers)/i", $_SERVER ['HTTP_USER_AGENT'] )) {
			define ( 'IS_ROBOT', FALSE );
		} elseif (preg_match ( "/($kw_spiders)/i", $_SERVER ['HTTP_USER_AGENT'] )) {
			define ( 'IS_ROBOT', TRUE );
		} else {
			define ( 'IS_ROBOT', FALSE );
		}
	}
	return IS_ROBOT;
}
function _getenv($var_name) {
	if (isset ( $_SERVER [$var_name] )) {
		return $_SERVER [$var_name];
	} elseif (isset ( $_ENV [$var_name] )) {
		return $_ENV [$var_name];
	} elseif (getenv ( $var_name )) {
		return getenv ( $var_name );
	} elseif (function_exists ( 'apache_getenv' ) && apache_getenv ( $var_name, true )) {
		return apache_getenv ( $var_name, true );
	}
	return '';
}

function getUsedMemory() {
	$memory_amount = memory_get_usage ();
	return number_format ( $memory_amount / 1024, 2 );
}
function getUsedTime() {
	global $thread_starttime;
	$mtime = explode ( ' ', microtime () );
	$thread_endtime = (( float ) $mtime [1] + ( float ) $mtime [0]);
	return number_format ( ($thread_endtime - $thread_starttime), 2 );
}
function nl_addslashes($string, $force = 0) {
	! defined ( 'MAGIC_QUOTES_GPC' ) && define ( 'MAGIC_QUOTES_GPC', get_magic_quotes_gpc () );
	if (! MAGIC_QUOTES_GPC || $force) {
		if (is_array ( $string )) {
			foreach ( $string as $key => $val ) {
				$string [$key] = nl_addslashes ( $val, $force );
			}
		} else {
			$string = addslashes ( $string );
		}
	}
	return $string;
}
function htmlencode($string, $clear_rn = false) {
	if (is_array ( $string )) {
		foreach ( $string as $key => $val ) {
			$string [$key] = htmlencode ( $val );
		}
	} else {
		if ($clear_rn) {
			$string = str_replace ( array ("\n", "\r" ), array ('', '' ), $string );
		}
		$string = preg_replace ( '/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1', str_replace ( array ('&', '"', '<', '>' ), array ('&amp;', '&quot;', '&lt;', '&gt;' ), $string ) );
	
	}
	return $string;
}
function nl_checkdate($ymd, $sep = '-') {
	if (! empty ( $ymd )) {
		list ( $year, $month, $day ) = explode ( $sep, $ymd );
		return checkdate ( $month, $day, $year );
	} else {
		return FALSE;
	}
}
function nl_cutstr($string, $length, $dot = ' ...', $htmlencode = true, $charset = 'utf-8') {
	if (strlen ( $string ) <= $length) {
		return $string;
	}
	if ($htmlencode)
		$string = str_replace ( array ('&amp;', '&quot;', '&lt;', '&gt;' ), array ('&', '"', '<', '>' ), $string );
	$strcut = '';
	if (strtolower ( $charset ) == 'utf-8') {
		$n = $tn = $noc = 0;
		while ( $n < strlen ( $string ) ) {
			$t = ord ( $string [$n] );
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1;
				$n ++;
				$noc ++;
			} elseif (194 <= $t && $t <= 223) {
				$tn = 2;
				$n += 2;
				$noc += 2;
			} elseif (224 <= $t && $t < 239) {
				$tn = 3;
				$n += 3;
				$noc += 2;
			} elseif (240 <= $t && $t <= 247) {
				$tn = 4;
				$n += 4;
				$noc += 2;
			} elseif (248 <= $t && $t <= 251) {
				$tn = 5;
				$n += 5;
				$noc += 2;
			} elseif ($t == 252 || $t == 253) {
				$tn = 6;
				$n += 6;
				$noc += 2;
			} else {
				$n ++;
			}
			if ($noc >= $length) {
				break;
			}
		}
		if ($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr ( $string, 0, $n );
	} else {
		for($i = 0; $i < $length; $i ++) {
			$strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
		}
	}
	if ($htmlencode)
		$strcut = str_replace ( array ('&', '"', '<', '>' ), array ('&amp;', '&quot;', '&lt;', '&gt;' ), $strcut );
	return $strcut . $dot;
}

function Unicode10Char($ord) {
	if ($ord < 0x7F) { // 0000-007F
		$c .= chr ( $ord );
	} elseif ($ord < 0x800) { // 0080-0800
		$c .= chr ( 0xC0 | ($ord / 64) );
		$c .= chr ( 0x80 | ($ord % 64) );
	} else { // 0800-FFFF
		$c .= chr ( 0xE0 | (($ord / 64) / 64) );
		$c .= chr ( 0x80 | (($ord / 64) % 64) );
		$c .= chr ( 0x80 | ($ord % 64) );
	}
	return $c;
}
function Unicode($string, $html_ntity_decode = false) {
	if (is_array ( $string )) {
		foreach ( $string as $key => $value ) {
			$string [$key] = Unicode ( $value );
		}
		return $string;
	} else {
		if (! $html_ntity_decode)
			return preg_replace ( "/(\&\#)(\d+)(;)/ies", "Unicode10Char(\\2)", $string );
		else
			return htmlspecialchars_decode ( preg_replace ( "/(\&\#)(\d+)(;)/ies", "Unicode10Char(\\2)", $string ) );
	}
}
//+++++JS助手+++++++++++
//调用js助手直接打印一个提示信息
function alert($message) {
	echo ("<script language='javascript';charset='utf8'>alert('" . $message . "')</script>");
	echo ("die ()");
}

//调用js助手打印提示信息并返回上一步
function alertandback($message) {
	echo ("<script language='javascript';charset='utf8'>alert('" . $message . "');history.back();</script>");
	echo "die ()";
}

//调用js助手打印提示信息并转向新的网页地址
function alertandgotopage($message, $url) {
	echo ("<script language='javascript';charset='utf8'>alert('" . $message . "');window.location.replace('" . $url . "');</script>");
	echo "die ()";
}
//跳转助手
function tourl($tourl) {
	echo "<script language=javascript>";
	echo "window.location='" . $tourl . "';";
	echo "</script>";
	echo "die()";
}

//以下为正则表达时函数匹配
//匹配电子邮件
function isemail($email) {
	return strlen ( $email ) > 8 && preg_match ( "/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+([a-z]{2,4})|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email );
}
function _isemail($email) {
	return strlen ( $email ) > 6 && preg_match ( "/^[\w\-\.]+@[\w\-]+(\.\w+)+$/", $email );
}
//匹配域名/网址
function isurl($url) {
	return preg_match ( "/(www.|https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/){1}([^\[\"']+?)/ie", $url );
}
//匹配数字和下划线
function isdigital($email) {
	return preg_match ( "/^[0-9\-]+$/", $email );
}
//匹配数字
function _isdigital($str) {
	return preg_match ( "/^[0-9]+$/", $str );
}
//设置缓存
function setCaches($prefix,$key,$cnt,$time)
{
	$key = md5 ( $key );
	$key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
	$tmp ['data'] = $value;
	$tmp ['timeout'] = time () + ( int ) $timeout;
	$cache_data = "<?php exit('dedecms');?>\n\r".@serialize ( $tmp );
	return @PutFile ( SYSTEM_ROOTPATH . "/cache/$prefix/$key.php",  $cache_data);
	
}


//获取缓存
function getCaches($prefix, $key)
{ 
	$key = md5 ( $key );
	$key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
    $result = @file_get_contents ( SYSTEM_ROOTPATH . "/cache/$prefix/$key.php" );
    if ($result === false)
    {
        return false;
    }
    $result = str_replace("<?php exit('dedecms');?>\n\r", "", $result);
    $result = @unserialize ( $result );
    if (empty ( $result ['timeout'] ) || $result ['timeout'] < time ())
    {
        return false;
    }
    return $result ['data'];
}
?>
